import 'dart:convert';

import 'package:largefriends/entity/json/json_parse_utils.dart';

class LfLocationEntity {
  LfLocationEntity({
    this.city,
    this.cityId,
    this.country,
    this.countryId,
    this.curAddress,
    this.state,
    this.stateId,
    this.latitude,
    this.longitude,
    this.stateAbbr,
    this.countAbbr,
  });

  factory LfLocationEntity.fromJson(Map<dynamic, dynamic>? jsonRes) =>
      LfLocationEntity(
        city: jsonRes?.asStrings(['city', 'curCity']),
        cityId: jsonRes.asStrings(['cityId', 'curCityId']),
        country: jsonRes.asStrings(['country', 'curCountry']),
        countryId: jsonRes.asStrings(['countryId', 'curCountryId']),
        curAddress: jsonRes.asString('curAddress'),
        state: jsonRes.asStrings(['state', 'curState']),
        stateId: jsonRes.asStrings(['stateId', 'curStateId']),
        countAbbr: jsonRes.asStrings(['countAbbr', 'countAbbr']),
        stateAbbr: jsonRes.asString('stateAbbr'),
        longitude: jsonRes.asString('longitude'),
        latitude: jsonRes.asString('latitude'),
      );

  String? city;
  String? cityId;
  String? country;
  String? countryId;
  String? curAddress;
  String? state;
  String? stateId;
  String? latitude;
  String? longitude;
  String? stateAbbr;
  String? countAbbr;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'city': city,
        'cityId': cityId,
        'country': country,
        'countryId': countryId,
        'curAddress': curAddress,
        'state': state,
        'stateId': stateId,
        'longitude': longitude,
        'latitude': latitude,
        'stateAbbr': stateAbbr,
        'countAbbr': countAbbr,
      };

  String toAddress() {
    return [city, state, country]
        .where((element) => element != null && element.isNotEmpty)
        .join(", ");
  }

  ///判断地址是否已经填写忘了
  bool hasCompleteInfo() {
    return city?.trim().isNotEmpty == true &&
        country?.trim().isNotEmpty == true &&
        state?.trim().isNotEmpty == true;
  }

  String currentAddress() {
    return curAddress ?? '';
  }

  LfLocationEntity clone() => LfLocationEntity.fromJson(toJson());
}
